/* 
 *
 * Author: Allan <qimingos_lsk@163.com>, (C) 2005-2007
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */
#include "include/ShowLyric.h"
#include "include/ConfigDlg.h"

GladeXML *g_xml;

GtkWidget* ConfigDlgInit(GladeXML *xml)
{
	g_xml = xml;
	GtkWidget* pConfigDlg = glade_xml_get_widget(g_xml, "ConfigDlg");
	XML_CONNECT_SIGNAL2(g_xml, on_fcbLyricPath_file_set, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbSearchApp_changed, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbBgColor_color_set, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbLyricColor_color_set, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbCurrColor_color_set, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_fbLyricFont_font_set, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_fbLyricFont_font_set, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbSmartShowWindow_toggled, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbSmartDownload_toggled, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_hsOpacity_value_changed, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_btnDefaultConfig_clicked, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_btnCloseConfig_clicked, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_ConfigDlg_show, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_ConfigDlg_delete_event, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbSaveLyricToSongFolder_toggled, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbShowOSD_toggled, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbShowWindowLyric_toggled, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbOSDHorizontalPos_changed, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbOSDVerticalPos_changed, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_fbOSDFont_font_set, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbShowOSDBg_toggled, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_entryLyricEncode_focus_out_event, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbKaLaOK_toggled, pConfigDlg);
	XML_CONNECT_SIGNAL2(g_xml, on_cbSaveLyricAsFileName_toggled, pConfigDlg);
	return pConfigDlg;
}

void on_ConfigDlg_show(GtkWidget *widget, gpointer user_data)
{
	theApp.m_player.LoadConfigs(&theApp.m_configs);

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(
			g_xml, "cbSmartDownload")), theApp.m_configs.bSmartDownLoad);

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(
			g_xml, "cbSaveLyricAsFileName")), theApp.m_configs.bSaveLyricAsFileName);

	gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(glade_xml_get_widget(
			g_xml, "fcbLyricPath")), theApp.m_configs.szLyricPath);

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(
			g_xml, "cbSmartShowWindow")), theApp.m_configs.bSmartShowWin);

	gtk_entry_set_text(GTK_ENTRY(glade_xml_get_widget(
			g_xml, "entryLyricEncode")), theApp.m_configs.szLyricEncode);

	gint nCount = theApp.m_player.GetSearchAppCount();
	gint nIndex = 0;
	AppInfo info = {0};
	for (nIndex == 0; nIndex < nCount; nIndex++)
	{
		theApp.m_player.GetSearchApp(nIndex, &info);
		gtk_combo_box_append_text(GTK_COMBO_BOX(glade_xml_get_widget(g_xml, "cbSearchApp")), info.szName);
	}
	gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(g_xml, "cbSearchApp")), theApp.m_configs.nSearchAppIndex);
	//----------------------------------------------------------------------------------------------------
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "fcbLyricPath"),
				!theApp.m_configs.bSaveLyricToSongFolder);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(
			g_xml, "cbSaveLyricToSongFolder")), theApp.m_configs.bSaveLyricToSongFolder);
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(
			g_xml, "cbShowWindowLyric")), theApp.m_configs.bShowWindowLyric);
	gtk_color_button_set_color(GTK_COLOR_BUTTON(glade_xml_get_widget(
			g_xml, "cbLyricColor")), &theApp.m_configs.colors.normal);
	gtk_color_button_set_color(GTK_COLOR_BUTTON(glade_xml_get_widget(
			g_xml, "cbCurrColor")), &theApp.m_configs.colors.current);

	gtk_font_button_set_font_name(GTK_FONT_BUTTON(glade_xml_get_widget(
			g_xml, "fbLyricFont")), theApp.m_configs.szLyricFontName);

	GtkWidget* phsOpacity = glade_xml_get_widget(g_xml, "hsOpacity");
	gtk_range_set_range(GTK_RANGE(phsOpacity), 0, 100);
	gtk_range_set_value(GTK_RANGE(phsOpacity), theApp.m_configs.iOpacity);

	//----------------------------------------------------------------------------------------------------
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbLyricColor"),
				theApp.m_configs.bShowWindowLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbCurrColor"),
				theApp.m_configs.bShowWindowLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "fbLyricFont"),
				theApp.m_configs.bShowWindowLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "hsOpacity"),
				theApp.m_configs.bShowWindowLyric);
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(
			g_xml, "cbShowOSD")), theApp.m_configs.bShowOSDLyric);

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(
				g_xml, "cbShowOSDBg")), strcmp(theApp.m_configs.OSD.fake_translucent_bg, "on") == 0);

	if (strcmp(theApp.m_configs.OSD.vposition, "top") == 0)
	{
		gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(g_xml, "cbOSDVerticalPos")), 0);
	}
	else if (strcmp(theApp.m_configs.OSD.vposition, "center") == 0)
	{
		gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(g_xml, "cbOSDVerticalPos")), 1);
	}
	else if (strcmp(theApp.m_configs.OSD.vposition, "bottom") == 0)
	{
		gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(g_xml, "cbOSDVerticalPos")), 2);
	}

	if (strcmp(theApp.m_configs.OSD.halignment, "left") == 0)
	{
		gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(g_xml, "cbOSDHorizontalPos")), 0);
	}
	else if (strcmp(theApp.m_configs.OSD.halignment, "center") == 0)
	{
		gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(g_xml, "cbOSDHorizontalPos")), 1);
	}
	else if (strcmp(theApp.m_configs.OSD.halignment, "right") == 0)
	{
		gtk_combo_box_set_active(GTK_COMBO_BOX(glade_xml_get_widget(g_xml, "cbOSDHorizontalPos")), 2);
	}

	gtk_font_button_set_font_name(GTK_FONT_BUTTON(glade_xml_get_widget(
			g_xml, "fbOSDFont")), theApp.m_configs.OSD.szFontName);

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(
			g_xml, "cbKaLaOK")), theApp.m_configs.bIsKaLaOK);
	//----------------------------------------------------------------------------------------------------
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbShowOSDBg"),
				theApp.m_configs.bShowOSDLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbOSDVerticalPos"),
				theApp.m_configs.bShowOSDLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbOSDHorizontalPos"),
				theApp.m_configs.bShowOSDLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "fbOSDFont"),
				theApp.m_configs.bShowOSDLyric);
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
}


void on_fcbLyricPath_file_set(GtkFileChooserButton *filechooserbutton,
		gpointer user_data)
{
	gchar* lpszPath =
			gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filechooserbutton));
	if (lpszPath)
	{
		strcpy(theApp.m_configs.szLyricPath, lpszPath);
		g_free(lpszPath);
		lpszPath = NULL;
	}
}

void on_cbBgColor_color_set(GtkColorButton *widget, gpointer user_data)
{
	gtk_color_button_get_color(widget, &theApp.m_configs.colors.background);
	theApp.UpdataSetting();
}

void on_cbLyricColor_color_set(GtkColorButton *widget, gpointer user_data)
{
	gtk_color_button_get_color(widget, &theApp.m_configs.colors.normal);
	theApp.UpdataSetting();
}

void on_cbCurrColor_color_set(GtkColorButton *widget, gpointer user_data)
{
	gtk_color_button_get_color(widget, &theApp.m_configs.colors.current);
	theApp.UpdataSetting();
}

void on_fbLyricFont_font_set(GtkFontButton *widget, gpointer user_data)
{
	strcpy(theApp.m_configs.szLyricFontName,
			gtk_font_button_get_font_name(GTK_FONT_BUTTON(widget)));
	theApp.UpdataSetting();
}

void on_cbSmartShowWindow_toggled(GtkToggleButton *togglebutton,
		gpointer user_data)
{
	theApp.m_configs.bSmartShowWin = gtk_toggle_button_get_active(togglebutton);
	theApp.UpdataSetting();
}

void on_cbSmartDownload_toggled(GtkToggleButton *togglebutton,
		gpointer user_data)
{
	theApp.m_configs.bSmartDownLoad = gtk_toggle_button_get_active(togglebutton);
	theApp.UpdataSetting();
}

void on_cbSaveLyricToSongFolder_toggled(GtkToggleButton *togglebutton,
		gpointer user_data)
{
	theApp.m_configs.bSaveLyricToSongFolder = gtk_toggle_button_get_active(togglebutton);

	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "fcbLyricPath"),
				!theApp.m_configs.bSaveLyricToSongFolder);
	theApp.UpdataSetting();
}

void on_cbShowOSD_toggled(GtkToggleButton *button, gpointer user_data)
{
	theApp.m_configs.bShowOSDLyric = gtk_toggle_button_get_active(button);

	if (theApp.m_configs.bShowOSDLyric == FALSE)
	{
		system("gnome-osd-client -f \"<message id='ShowLyric' hide_timeout='0'></message>\"");
	}
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbShowOSDBg"),
				theApp.m_configs.bShowOSDLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbOSDVerticalPos"),
				theApp.m_configs.bShowOSDLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbOSDHorizontalPos"),
				theApp.m_configs.bShowOSDLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "fbOSDFont"),
				theApp.m_configs.bShowOSDLyric);
	theApp.UpdataSetting();
}

void on_hsOpacity_value_changed(GtkRange *range, gpointer user_data)
{
	theApp.m_configs.iOpacity = (int)gtk_range_get_value(range);
	theApp.UpdataSetting();
}

void on_btnDefaultConfig_clicked(GtkButton *button, gpointer user_data)
{
	theApp.GetDefautConfigs(&theApp.m_configs);
	GtkWidget* pConfigDlg = glade_xml_get_widget(g_xml, "ConfigDlg");
	theApp.UpdataSetting();
	on_ConfigDlg_show(GTK_WIDGET(pConfigDlg), pConfigDlg);
}

void on_btnCloseConfig_clicked(GtkButton *button, gpointer user_data)
{
	gtk_widget_hide(GTK_WIDGET(user_data));
}

gboolean on_ConfigDlg_delete_event(GtkWidget *widget, GdkEvent *event,
		gpointer user_data)
{
	// 将关闭事件改成隐藏
	gtk_widget_hide(GTK_WIDGET(user_data));

	// 如果返回FALSE，GTK将发出"destroy"信号;
	// 如果返回TRUE， 则不让该窗口关闭
	return TRUE;
}

void on_cbKaLaOK_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
	theApp.m_configs.bIsKaLaOK = gtk_toggle_button_get_active(togglebutton);
	theApp.UpdataSetting();
}

void on_cbSaveLyricAsFileName_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
	theApp.m_configs.bSaveLyricAsFileName = gtk_toggle_button_get_active(togglebutton);
	theApp.UpdataSetting();
}

void on_cbShowWindowLyric_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
	theApp.m_configs.bShowWindowLyric = gtk_toggle_button_get_active(togglebutton);

	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbLyricColor"),
				theApp.m_configs.bShowWindowLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "cbCurrColor"),
				theApp.m_configs.bShowWindowLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "fbLyricFont"),
				theApp.m_configs.bShowWindowLyric);
	gtk_widget_set_sensitive (glade_xml_get_widget(g_xml, "hsOpacity"),
				theApp.m_configs.bShowWindowLyric);

	theApp.m_LyricWnd.SmartShowWnd(theApp.m_configs.bShowWindowLyric);

	theApp.UpdataSetting();
}
void on_cbOSDHorizontalPos_changed(GtkComboBox * comboBox, gpointer user_data)
{
	switch (gtk_combo_box_get_active(comboBox))
	{
	case 0:
		strcpy(theApp.m_configs.OSD.halignment, "left");
		break;
	case 1:
		strcpy(theApp.m_configs.OSD.halignment, "center");
		break;
	case 2:
		strcpy(theApp.m_configs.OSD.halignment, "right");
		break;
	}
	theApp.UpdataSetting();
}
void on_cbOSDVerticalPos_changed(GtkComboBox * comboBox, gpointer user_data)
{
	switch (gtk_combo_box_get_active(comboBox))
	{
	case 0:
		strcpy(theApp.m_configs.OSD.vposition, "top");
		break;
	case 1:
		strcpy(theApp.m_configs.OSD.vposition, "center");
		break;
	case 2:
		strcpy(theApp.m_configs.OSD.vposition, "bottom");
		break;
	}
	theApp.UpdataSetting();
}
void on_fbOSDFont_font_set(GtkFontButton * widget, gpointer user_data)
{
	strcpy(theApp.m_configs.OSD.szFontName,
			gtk_font_button_get_font_name(GTK_FONT_BUTTON(widget)));
	theApp.UpdataSetting();
}
void on_cbShowOSDBg_toggled(GtkToggleButton * togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active(togglebutton))
	{
		strcpy(theApp.m_configs.OSD.fake_translucent_bg, "on");
	}
	else
	{
		strcpy(theApp.m_configs.OSD.fake_translucent_bg, "off");
	}
	theApp.UpdataSetting();
}

void on_entryLyricEncode_focus_out_event(GtkEntry * entry, gpointer user_data)
{
	strncpy(theApp.m_configs.szLyricEncode, gtk_entry_get_text(entry), MAX_PATH);
	theApp.UpdataSetting();
}

void on_cbSearchApp_changed(GtkComboBox * comboBox, gpointer user_data)
{
	theApp.m_configs.nSearchAppIndex = gtk_combo_box_get_active(comboBox);
	theApp.UpdataSetting();
}
